var requestmodule_src='';
function __AB_Requestmodule_Src(location_src){
	requestmodule_src=location_src;
}

jQuery(document).ready(function(){

	jQuery("#from_time").timespinner({  
		showSeconds:true ,
		showSeconds: false,
		highlight: 0,
		min: "08:00",
		max: "20:00"
	});  

	jQuery("#time_length").numberspinner({  
		editable:true,
		increment: 30,
		width: 50,
		spin: function(value){
			var spin_option=jQuery(this).spinner("options");
			var val=parseInt(spin_option.value);
			if (value)
				val-=spin_option.increment;
			else
				val+=spin_option.increment;

			if (val <spin_option.min)
				val=spin_option.min;
			if (val > spin_option.max)
				val=spin_option.max;
			// validate time_length
			var bH=jQuery("#from_time").timespinner("getHours");
			var bM=jQuery("#from_time").timespinner("getMinutes");
			// limit to 23h
			if (bH*60+bM+val > 1320 )
			{
				val=1320-bM-bH*60;
			}
			//
			spin_option.value=val;
			jQuery(this).val(val);
			jQuery(this).spinner("options", spin_option);
			//
			_AB_RequestModule_UpdateOnChange();
		}
	});

	jQuery("#cat_id").combobox({
		onSelect: function(){
			_AB_RequestModule_UpdateOnChange();
		} 
	});

	_AB_RequestModule_UpdateOnChange(); // first update :)

	// form submit
	jQuery("#request_contact").submit(function(){
		if (!jQuery(this).valid())
			return;
		
		// check register time must after 30mins after Now
		//new Date(year, month, day, hours, minutes, seconds, milliseconds)
		var sdate=jQuery("#from_date").datebox("getValue").match(/(\d+)/g);
		var bH=jQuery("#from_time").timespinner("getHours");
		var bM=jQuery("#from_time").timespinner("getMinutes");
		var tDate=new Date(parseInt(sdate[2]), parseInt(sdate[0])-1, parseInt(sdate[1]), bH, bM, 0,0);
		var nDate=new Date(); 
		nDate=new Date(nDate.getTime()+30*60*1000) ;// sooner 30mins
		if (nDate>tDate)
		{
			alert("Thời điểm đặt thuê không được ở trong quá khứ");
			return false;
		}
		
		jQuery("#request_contact .control .button").fadeToggle();
		_abpv_showLoading();

		jQuery.post('/index.php',jQuery(this).serialize(), function(data) {
			data=ajax_string_dim(data);
			$.messager.show({
				title:'Cảm ơn bạn đã đăng ký thuê phòng tập',
				msg:data,
				timeout:5000,
				showType:'slide'
			});

			// clear data
			jQuery("input[name='fullname']").val("");
			jQuery("input[name='phone']").val("");
			jQuery("input[name='email']").val("");
			jQuery("textarea[name='other_comment']").val("");
			_abpv_hideLoading();
			jQuery("#request_contact .control .button").fadeToggle();
		});
		return false;
	});


});


/**
* This function for cat on change and time_length on change
* @author trungdt
* @since Nov 9, 2011
*/
function _AB_RequestModule_UpdateOnChange(){
	var time_length=parseInt(jQuery("#time_length").numberspinner("getValue"));
	var cat_id=parseInt(jQuery("#cat_id").combobox("getValue"));

	// for time_length_span
	var ret="";
	var h=Math.floor(time_length/60);
	var m=(time_length%60);
	ret+=h+" giờ";
	if (m>0)
		ret+=" "+m+" phút";
	jQuery('#time_length_span').html(ret);
	// summary span
	var car_price=0;
	for (var i=0;i<cats.length;i++)
	if (cats[i].id == cat_id)
		{
		cat_price=cats[i].cat_value;
		break;
	}
	time_length=Math.round(parseFloat(time_length)/60*cat_price,2);
	jQuery("#summary_span").html("Khoảng "+DocTienBangChu(time_length)+" đồng");
	jQuery("input[name='tien_tam_tinh']").val(jQuery("#summary_span").html());
}

